package com.baseinfo.service.impl;

import com.model.TInstitutions;
import com.baseinfo.service.InstitutionsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.framework.util.StringUtil;
import com.framework.base.dao.impl.BaseDaoImpl;

/**
 * @author admin
 * @date
 */
@Service
public class InstitutionsServiceImpl implements InstitutionsService {
    @Resource(name = "baseDaoImpl")
    private BaseDaoImpl baseDao;

    @Override
    public TInstitutions save (TInstitutions modelParam) {
        TInstitutions model = null;
        if (modelParam != null) {
            try {
                model = (TInstitutions) this.baseDao.savePo(modelParam);
            } catch (Exception e) {
                System.out.println("更新失败");
            }
        }
        return model;
    }

    @Override
    public TInstitutions saveOrupdate (TInstitutions modelParam) {
        TInstitutions model = null;
        if (modelParam != null) {
            try {
                model = (TInstitutions) this.baseDao.saveOrupdate(modelParam);
            } catch (Exception e) {
                System.out.println("更新失败");
            }
        }
        return model;
    }

    @Override
    public List<TInstitutions> findAll (Map<String,Object> map) {
        String params = "";
        StringBuffer hql = new StringBuffer("from TInstitutions c where 1=1 and c.isDeleted=0");
        if (map != null) {
            params = this.SQLAddParams(map);
            hql = hql.append(params);
        }
        List<TInstitutions> list = null;
        try {
            list = this.baseDao.listByHql(hql.toString(), null);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("条件查找失败");
        }
        return list;
    }

    @Override
    public TInstitutions findById (Long id) {
        TInstitutions model = null;
        List<Object> params = new ArrayList<Object>();
        if (id != null) {
            StringBuilder hsql = new StringBuilder("from TInstitutions c where 1=1 and c.isDeleted=0 ");
            params.add(id);
            hsql.append(" and c.id=?");
            model = (TInstitutions) this.baseDao.getSingleByHsql(hsql.toString(), params);
        } else {
            System.out.println("查找不到ID=" + id + "的数据");
        }
        return model;
    }

    @Override
    public void del (Long id) {
        List<Object> params = new ArrayList<Object>();
        if (id != null) {
            params.add(id);
            try {
                TInstitutions model = (TInstitutions) this.baseDao.getSingleByHsql(" from TInstitutions c where c.id=? and c.isDeleted=0 ", params);
                model.setIsDeleted(1);
                this.baseDao.saveOrupdate(model);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 搜索SQL加参数条件（抽出来复用）
     *
     * @param map
     * @return
     */
    public String SQLAddParams (Map<String,Object> map) {
        StringBuffer bf = new StringBuffer("");
        if (map.containsKey("name") && StringUtil.isNotEmpty(map.get("name").toString())) {
            bf.append(" and c.name like '%").append(map.get("name").toString()).append("%'");
        }
        if (map.containsKey("type") && StringUtil.isNotEmpty(map.get("type").toString())) {
            bf.append(" and c.type=").append(map.get("type").toString());
        }
        if (map.containsKey("status") && StringUtil.isNotEmpty(map.get("status").toString())) {
            bf.append(" and c.status=").append(map.get("status").toString());
        }
        if (map.containsKey("createTime") && StringUtil.isNotEmpty(map.get("createTime").toString())) {
            String[] array = map.get("createTime").toString().split("~");
            bf.append(" and c.createTime>=").append(array[0]);
            bf.append(" and c.createTime<=").append(array[1]);
        }
        return bf.toString();
    }

    /**
     * 根据元素全局查询(分页)
     */
    @Override
    public List<TInstitutions> conditionsPage (Map<String,Object> map, Long start, Long length) {
        String params = this.SQLAddParams(map);
        StringBuffer hql = new StringBuffer("from TInstitutions c where 1=1 and c.isDeleted=0 ");
        hql = hql.append(params);
        List<TInstitutions> list = null;
        try {
            list = this.baseDao.listByHqlBeatch(hql.toString(), start, length);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("条件查找用户失败");
        }
        return list;
    }

    /**
     * 获取总记录数
     */
    @Override
    public Long getTotalSize () {
        return this.baseDao.getTotalSize("select count(c.id) from TInstitutions c where 1=1 and c.isDeleted=0", null);
    }

    /**
     * 分页查询
     *
     * @param start  第几页
     * @param length 一页几行
     * @return
     */
    @Override
    public List<TInstitutions> searchPage (Long start, Long length) {
        return this.baseDao.listByHqlBeatch("from TInstitutions c where 1=1 and c.isDeleted=0 ", start, length);
    }

    /**
     * 获取条件搜索到的条目数
     */
    @Override
    public Long getSearchSize (Map<String,Object> map) {
        String params = this.SQLAddParams(map);
        StringBuffer hql = new StringBuffer("select count(c.id) from TInstitutions c where 1=1 and c.isDeleted=0");
        hql.append(params);
        return this.baseDao.getTotalSize(hql.toString(), null);
    }
}